﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public class AddressBLL : IAddressBLL
    {
        private static readonly IAddressDAL dal = DataAccess.CreateAddress();

        public DataSet LoadEmployeeAddress(int EmpSequence, Nullable<int> AddressType)
        {
            return dal.LoadEmployeeAddress(EmpSequence, AddressType);
        }
        public void InsertAddressContact(int EmployeeSeq, string Address, string District, string City, string Country
            , string PostalCode, string Tel, string Fax, string Email, int CreatedBy)
        {
            EmployeeInfo empInfo = new EmployeeInfo();
            MasDistrictInfo disInfo = new MasDistrictInfo();
            MasCityInfo cityInfo = new MasCityInfo();
            MasCountryInfo counInfo = new MasCountryInfo();
            AddressInfo info = new AddressInfo();

            empInfo.Sequence = EmployeeSeq;
            disInfo.Sequence = string.IsNullOrEmpty(District) ? (int?)null : Int32.Parse(District);
            cityInfo.Sequence = string.IsNullOrEmpty(City) ? 0 : Int32.Parse(City);
            counInfo.Sequence = string.IsNullOrEmpty(Country) ? (int?)null : Int32.Parse(Country);
            info.Employee = empInfo;
            info.AddressType = 1;
            info.Address = Address;
            info.District = disInfo;
            info.City = cityInfo;
            info.Country = counInfo;
            info.PostalCode = PostalCode;
            info.Phone = Tel;
            info.Fax = Fax;
            info.Email = Email;
            info.CreatedBy = CreatedBy;

            try
            {
                dal.InsertEmployeeAddress(info);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void InsertAddressHome(int EmployeeSeq, string Address, string District, string City, string Country
            , string PostalCode, string Tel, int CreatedBy)
        {
            EmployeeInfo empInfo = new EmployeeInfo();
            MasDistrictInfo disInfo = new MasDistrictInfo();
            MasCityInfo cityInfo = new MasCityInfo();
            MasCountryInfo counInfo = new MasCountryInfo();
            AddressInfo info = new AddressInfo();

            empInfo.Sequence = EmployeeSeq;
            disInfo.Sequence = string.IsNullOrEmpty(District) ? (int?)null : Int32.Parse(District);
            cityInfo.Sequence = string.IsNullOrEmpty(City) ? 0 : Int32.Parse(City);
            counInfo.Sequence = string.IsNullOrEmpty(Country) ? (int?)null : Int32.Parse(Country);
            info.Employee = empInfo;
            info.AddressType = 2;
            info.Address = Address;
            info.District = disInfo;
            info.City = cityInfo;
            info.Country = counInfo;
            info.PostalCode = PostalCode;
            info.Phone = Tel;
            info.CreatedBy = CreatedBy;

            try
            {
                dal.InsertEmployeeAddress(info);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void UpdateAddressContact(int Sequence, string Address, string District, string City, string Country
            , string PostalCode, string Tel, string Fax, string Email, int UpdatedBy)
        {
            MasDistrictInfo disInfo = new MasDistrictInfo();
            MasCityInfo cityInfo = new MasCityInfo();
            MasCountryInfo counInfo = new MasCountryInfo();
            AddressInfo info = new AddressInfo();

            info.Sequence = Sequence;
            info.AddressType = 1;
            disInfo.Sequence = string.IsNullOrEmpty(District) ? (int?)null : Int32.Parse(District);
            cityInfo.Sequence = string.IsNullOrEmpty(City) ? 0 : Int32.Parse(City);
            counInfo.Sequence = string.IsNullOrEmpty(Country) ? (int?)null : Int32.Parse(Country);
            info.Address = Address;
            info.District = disInfo;
            info.City = cityInfo;
            info.Country = counInfo;
            info.PostalCode = PostalCode;
            info.Phone = Tel;
            info.Fax = Fax;
            info.Email = Email;
            info.UpdatedBy = UpdatedBy;

            try
            {
                dal.UpdateEmployeeAddress(info);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void UpdateAddressHome(int Sequence, string Address, string District, string City, string Country
            , string PostalCode, string Tel, int UpdatedBy)
        {
            MasDistrictInfo disInfo = new MasDistrictInfo();
            MasCityInfo cityInfo = new MasCityInfo();
            MasCountryInfo counInfo = new MasCountryInfo();
            AddressInfo info = new AddressInfo();

            info.Sequence = Sequence;
            info.AddressType = 2;
            disInfo.Sequence = string.IsNullOrEmpty(District) ? (int?)null : Int32.Parse(District);
            cityInfo.Sequence = string.IsNullOrEmpty(City) ? 0 : Int32.Parse(City);
            counInfo.Sequence = string.IsNullOrEmpty(Country) ? (int?)null : Int32.Parse(Country);
            info.Address = Address;
            info.District = disInfo;
            info.City = cityInfo;
            info.Country = counInfo;
            info.PostalCode = PostalCode;
            info.Phone = Tel;
            info.UpdatedBy = UpdatedBy;

            try
            {
                dal.UpdateEmployeeAddress(info);
            }
            catch
            {
                throw new Exception("");
            }
        }
    }
}
